package com.example.springbootrbac.controller;

import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.pojo.Result;
import com.example.springbootrbac.pojo.ResultCode;
import com.example.springbootrbac.service.QueryService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class QueryController {
    @Resource(name= "queryServiceImpl")
    QueryService queryService;
    @PostMapping("/index/announcement/query")
    public Result<List<AnnouncementEntity>> queryAnnouncement(String title,String createBy){
        System.out.println("前端参数:"+title+createBy);
        if (ObjectUtils.isEmpty(createBy)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"发布者不能为空哦");
        }
        List<AnnouncementEntity> token = queryService.queryAnnouncement(title,createBy);
          if (ObjectUtils.isEmpty(token)){
              return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"找不到相关数据呢");
          }
        return Result.success("公告更新成功",token);
    }

    @PostMapping("/index/room/query")
    public Result<List<RoomEntity>> queryRoom(String name,String quality,int available){
        System.out.println("参数："+name+"and "+quality+"and "+available);
        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(available)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"房屋或可入住数不能为空哦");
        }else if (available < 0){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"可入住数不能小于0哦");
        }
        List<RoomEntity> token = queryService.queryRoom(name,quality,available);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"找不到相关数据呢");
        }
        return Result.success("房屋信息更新成功",token);
    }

    @PostMapping("/index/course/query")
    public Result<List<CourseEntity>> queryCourse(String courseSql,String teacherSql){
        System.out.println("参数："+courseSql+teacherSql);
        if (ObjectUtils.isEmpty(courseSql) && ObjectUtils.isEmpty(teacherSql)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"要查询的课程名或教师不能为空哦");
        }
        List<CourseEntity> token = queryService.queryCourse(courseSql,teacherSql);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"找不到相关数据呢");
        }
        return Result.success("课程信息更新成功",token);
    }

    @PostMapping("/index/messageBoards/query")
    public Result<List<MessagesBoardEntity>> queryMessageBoard(String text,String createBy){
        System.out.println("前端参数:"+"2:"+text+"3:"+createBy);
        if (ObjectUtils.isEmpty(createBy)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"留言者不能为空哦");
        }
        List<MessagesBoardEntity> token = queryService.queryMessagesBoard(text,createBy);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"找不到相关数据呢");
        }
        return Result.success("留言板更新成功",token);
    }
    @PostMapping("/index/member/query")
    public Result<List<UserEntity>> queryRoom(String username,String status, HttpServletRequest request){
        System.out.println("参数："+username+status);
        List<UserEntity> token = queryService.queryUser(username,status,request);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_QUERY_ERROR,"找不到相关数据呢");
        }
        return Result.success("人员信息更新成功",token);
    }
}
